#!/bin/bash
export LANG="en_US.UTF-8"
source /etc/profile
getPortConfig(){
  args=($@)
  echo `sed -n "/${arg1}=/"p ${portConfig} | sed "s/${arg1}=//"`
}
baseDir="/opt/app/spring"
portConfig="/opt/app/appPorts.properties"
arg1=${2}
export portStart="`getPortConfig`"
VerSion=1.00

function PortsCheck()
{
echo "    |--->[√]启动端口号是:$portStart"
if      
    [ ! $portStart  ]
      then     
        echo "[×]发布脚本获取${2}端口失败,请检查传参！本次发布被取消！无需回滚"
        exit 1
fi
echo ""
}


function ShutdownProject()
{
processpid=`ps -ef | grep -v grep | grep -v $0 | grep java | grep ${2} | awk '{print $2}'`
if [ X"$processpid" = X ];then
    echo "进程不存在,请注意"
else
    kill -9 $processpid
    if [ $? -eq 0 ];then
        echo "    |--->[√]关闭${2}系统成功..."
    else
        echo "关闭${2}系统失败，请检查..."
        echo "脚本退出"
        exit 2
    fi
fi
echo ""
}

function StartProject()
{
echo "    |--->后台启动进程"
nohup java ${3} ${4} -Xss256K -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:SurvivorRatio=3 -jar $baseDir/running/$1/*.jar --spring.profiles.active=$5 --server.port=$portStart > /dev/null &
echo $! >  ${baseDir}/running/$1/main.pid
sleep 5
ps -ef | grep -v grep | grep -v $0 | grep java | grep ${2} >> /dev/null
if [ $? -eq 0 ];then
    echo "        |--->[√]启动完成"
else
    echo "${2}进程找不到,启动失败,请检查!"
    exit 2   
fi
echo ""
}

if [ $# -eq 3 ];then
    ls /bin/dmesg >> /dev/null
    if [ $? -ne 0 ];then
        echo "/bin/dmesg 命令不存在,无法检测服务器是否为虚拟机,此时采用默认的参数: -Xms6g -Xmx6g"
        echo "如果已知是物理机请传递相应的参数。"
        echo "虚拟机默认jvm参数为：-Xms6g -Xmx6g"
        echo "物理机默认jvm参数为：-Xms6g -Xmx6g"
        
        jvm1="-Xms6g"
        jvm2="-Xmx6g"
    else
        /bin/dmesg | grep -E "VMware|virtio-pci" >> /dev/null
        if [ $? -eq 0 ];then
            echo "此服务器为虚拟机"
            jvm1="-Xms6g"
            jvm2="-Xmx6g"
        else
            echo "此服务器为物理机"
            jvm1="-Xms6g"
            jvm2="-Xmx6g"
        fi
    fi

    ENV=$3 

elif [ $# -eq 5 ];then
    ls /bin/dmesg >> /dev/null
    if [ $? -eq 0 ];then
	/bin/dmesg | grep -E "VMware|virtio-pci" >> /dev/null
        if [ $? -eq 0 ];then
            echo "此服务器为虚拟机"
        else
            echo "此服务器为物理机"
        fi
    else
        echo "/bin/dmesg 命令不存在,无法检测服务器是否为虚拟机"
    fi

    jvm1=$3
    jvm2=$4
    ENV=$5

    echo $jvm1 | grep "\-Xms" >> /dev/null
    if [ $? -ne 0 ];then
        echo "$jvm1 参数错误，请检查!!!"
        exit 3
    fi 
    echo $jvm2 | grep "\-Xmx" >> /dev/null
    if [ $? -ne 0 ];then
        echo "$jvm2 参数错误, 请检查!!!"
        exit 4
    fi
else
    echo "参数错误，请检查"
    echo "启动失败"
    echo "脚本需要传递最多四个参数:"
    echo "./nettystart.sh dirpath/project_path projcet_path [-XmsNg -XmxMg] [pre|prd|sit|prod]"
    echo "[]中的参数可以省略，根据虚拟机还是物理机不同，虚拟机为-Xms6g -Xmx6g ，物理机为-Xms6g -Xmx6g"
    echo "[pre|prd|sit|prod]为启动参数，或者你认为正确定的一个参数"
    exit 3
fi

echo "---->1.1 获取端口号..."
PortsCheck $1 $2
echo "---->1.2 关闭$2系统"
ShutdownProject $1 $2
echo "---->1.3 开始运行服务"
StartProject  $1 $2 $jvm1 $jvm2 $ENV
